package com.zhf.mapper;

import com.zhf.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/11/14 15:51
 */
public interface UserMapper {

    /**
     * 查询用户
     */
    @Select("select * from user")
    public List<User> findAll();

    /**
     * 添加用户
     */
    @Insert("insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})")
    public void save(User user);

    /**
     * 更新用户
     */
    @Update("update user set username=#{username},birthday=#{birthday} where id=#{id}")
    public void update(User user);

    /**
     * 删除用户
     */
    @Delete("delete from user where id=#{id}")
    public void delete(int id);

    /**
     * 根据id查询用户
     */
    @Select("select * from user where id=#{uid}")
    public User findById(int uid);

    /**
     * 查询所有用户，及关联的订单信息
     */
    @Select("select * from user")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "username",column = "username"),
            @Result(property = "birthday",column = "birthday"),
            @Result(property = "sex",column = "sex"),
            @Result(property = "address",column = "address"),
            @Result(property = "ordersList",javaType = List.class,column = "id" ,many = @Many(select = "com.zhf.mapper.OrderMapper.findOrdersByUid"))
    })
    public List<User> findAllWithOrder();

    /**
     * 查询所有用户及关联的角色信息
     */
    @Select("select * from user")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "username",column = "username"),
            @Result(property = "birthday",column = "birthday"),
            @Result(property = "sex",column = "sex"),
            @Result(property = "address",column = "address"),
            @Result(property = "roleList" ,javaType = List.class,column = "id",many  =@Many(select = "com.zhf.mapper.RoleMapper.findById"))
    })
    public List<User> findAllWithRole();
}
